package com.zk.algorithm.array;

import com.zk.algorithm.Utils;
import com.zk.algorithm.annotation.Finished;

@Finished(false)
public class SortColors {

    public static void main(String...args) {
        int[] nums = new int[]{1};
        new SortColors().sortColors(nums);
    }

    public void sortColors(int[] nums) {
        for (int i=1; i<nums.length; i++) {
            int last = nums[i - 1];
            int curr = nums[i];

            // =====================
            // 需要移动
            // ...0 1 1 0...
            // ...0 1 1 2 2 0...
            // ...0 1 1 2 2 1...
            // =====================
            if (curr < last) {
                move(nums, i);
            }

            Utils.println(nums);
        }
    }

    private void move(int[] nums, int i) {
        int curr = nums[i];

        while (i - 1 >= 0 && nums[i - 1] > curr) {
            nums[i] = nums[i - 1];
            i--;
        }

        nums[i] = curr;
    }

}
